import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * file name:		TestUnzip.java
 * copyright:		© 2009 Kingsoft
 * author:			TanMenglong
 * version:			?.??
 * created:			2009-08-10
 * last modified:	2009-08-10
 * description:		HERE
 */

public class TestUnzip {
	public void unzip(InputStream is, OutputStream os) {
		try {
			ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
	
			int size;
			byte[] buffer = new byte[2048];
	
//			BufferedOutputStream bos = new BufferedOutputStream(os, buffer.length);
	
			while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
				os.write(buffer, 0, size);
			}
			os.flush();
			os.close();
			zis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void ungzip(InputStream is, OutputStream os) {
		try {
			GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(is));

			int size;
			byte[] buffer = new byte[2048];

			BufferedOutputStream bos = 
				new BufferedOutputStream(os, buffer.length);
			
			while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
				bos.write(buffer, 0, size);
			}
			bos.flush();
			bos.close();
			zis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
    public static void main(String[] args) {
		String source = "/dustbin/test/wss/dump/gzip";
		String dest = "/dustbin/test/wss/dump/out.txt";
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(source);
			fos = new FileOutputStream(dest);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		TestUnzip unzipper = new TestUnzip();
		unzipper.ungzip(fis, fos);
    }
}
